package kis.io.post;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class PostXml
{
	public static String sendPost(String url, File file, String input_coding)
	{
		String result = "";// 返回的结果
		BufferedReader in = null;// 读取响应输入流
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer();// 处理请求参数
		try
		{
			// 创建URL对象
			java.net.URL connURL = new java.net.URL(url);
			// 打开URL连接
			java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
			// 设置POST方式
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			// 获取HttpURLConnection对象对应的输出流
			out = new PrintWriter(httpConn.getOutputStream());
			// 发送请求参数
			// 从文件获得输入
			BufferedReader in1 = new BufferedReader(new InputStreamReader(new FileInputStream(file), input_coding));
			// 循环获得文件中的内容，并out发出
			String s;
			while ((s = in1.readLine()) != null)
			{
				System.out.println(s);
				out.println(s);

				out.flush();
			}
			out.close();
			System.out.println("已经成功发送XML文件!");
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应，设置编码方式
			in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "GBK"));
			String line;
			// 读取返回的内容
			while ((line = in.readLine()) != null)
			{
				result += line;
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (out != null)
				{
					out.close();
				}
				if (in != null)
				{
					in.close();
				}
			} catch (IOException ex)
			{
				ex.printStackTrace();
			}
		}
		return result;
	}
	public static void main(String[] args)
	{
		File file = new File("E:\\work\\workspace\\WGJACT\\src\\FindCar.xml");
//		File file = new File("E:\\work\\workspace\\WGJACT\\src\\rebel.xml");
		String result = sendPost("http://localhost:8080/WGJACT/servlet/HandleCenter", file, "GBK");
		System.out.println(result);
	}
}
